using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKHIS
    {
        public DataRow Convert2KHIS(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKHIS(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KHMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KHKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["KHLFDNR"] = " ".PadRight(0 ); 
         dwReturn["KHDATE"] = " ".PadRight(0 ); 
         dwReturn["KHDATEI"] = " ".PadRight(0 ); 
         dwReturn["KHTRTYPE"] = " ".PadRight(2 ); 
         dwReturn["KHVERS"] = " ".PadRight(2 ); 
         dwReturn["KHVERS2"] = " ".PadRight(2 ); 
         dwReturn["KHPOS"] = " ".PadRight(1 ); 
         dwReturn["KHKREDLIM"] = " ".PadRight(0 ); 
         dwReturn["KHKREDVON"] = " ".PadRight(0 ); 
         dwReturn["KHKREDBIS"] = " ".PadRight(0 ); 
         dwReturn["KHVERSTAGE"] = " ".PadRight(0 ); 
         dwReturn["KHVERSNR"] = " ".PadRight(10 ); 
         dwReturn["KHCOM1"] = " ".PadRight(70 ); 
         dwReturn["KHCOM2"] = " ".PadRight(70 ); 
         dwReturn["KHWVTERM"] = " ".PadRight(0 ); 
         dwReturn["KHFILLER"] = " ".PadRight(77 ); 
         dwReturn["KHAENZL"] = " ".PadRight(0 ); 
         dwReturn["KHAENB"] = " ".PadRight(8 ); 
         dwReturn["KHAEND"] = " ".PadRight(0 ); 
         dwReturn["KHNEUB"] = " ".PadRight(8 ); 
         dwReturn["KHNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKHIS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKHIS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKHIS(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKHIS(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKHIS(dwLocal, ITXDBVERSION);
            }
            UpdateKHIS(dwLocal);
        }
        public void InsertKHIS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KHIS(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKHIS(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KHIS WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KHIS  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KHIS";
            Return = Result;
            return Return;
        }
        public void DeleteKHIS(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KHIS   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKHIS(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KHIS SET "
                    +"KHMANDANT= :KHMANDANT,"
                    +"KHKUNDNR= :KHKUNDNR,"
                    +"KHLFDNR= :KHLFDNR,"
                    +"KHDATE= :KHDATE,"
                    +"KHDATEI= :KHDATEI,"
                    +"KHTRTYPE= :KHTRTYPE,"
                    +"KHVERS= :KHVERS,"
                    +"KHVERS2= :KHVERS2,"
                    +"KHPOS= :KHPOS,"
                    +"KHKREDLIM= :KHKREDLIM,"
                    +"KHKREDVON= :KHKREDVON,"
                    +"KHKREDBIS= :KHKREDBIS,"
                    +"KHVERSTAGE= :KHVERSTAGE,"
                    +"KHVERSNR= :KHVERSNR,"
                    +"KHCOM1= :KHCOM1,"
                    +"KHCOM2= :KHCOM2,"
                    +"KHWVTERM= :KHWVTERM,"
                    +"KHFILLER= :KHFILLER,"
                    +"KHAENZL= :KHAENZL,"
                    +"KHAENB= :KHAENB,"
                    +"KHAEND= :KHAEND,"
                    +"KHNEUB= :KHNEUB,"
                    +"KHNEUD= :KHNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KHMANDANT", OracleType.NVarChar).Value = dwData["KHMANDANT"].ToString(); 
          or1.Parameters.Add("KHKUNDNR", OracleType.NVarChar).Value = dwData["KHKUNDNR"].ToString(); 
          or1.Parameters.Add("KHLFDNR", OracleType.NVarChar).Value = dwData["KHLFDNR"].ToString(); 
          or1.Parameters.Add("KHDATE", OracleType.NVarChar).Value = dwData["KHDATE"].ToString(); 
          or1.Parameters.Add("KHDATEI", OracleType.NVarChar).Value = dwData["KHDATEI"].ToString(); 
          or1.Parameters.Add("KHTRTYPE", OracleType.NVarChar).Value = dwData["KHTRTYPE"].ToString(); 
          or1.Parameters.Add("KHVERS", OracleType.NVarChar).Value = dwData["KHVERS"].ToString(); 
          or1.Parameters.Add("KHVERS2", OracleType.NVarChar).Value = dwData["KHVERS2"].ToString(); 
          or1.Parameters.Add("KHPOS", OracleType.NVarChar).Value = dwData["KHPOS"].ToString(); 
          or1.Parameters.Add("KHKREDLIM", OracleType.NVarChar).Value = dwData["KHKREDLIM"].ToString(); 
          or1.Parameters.Add("KHKREDVON", OracleType.NVarChar).Value = dwData["KHKREDVON"].ToString(); 
          or1.Parameters.Add("KHKREDBIS", OracleType.NVarChar).Value = dwData["KHKREDBIS"].ToString(); 
          or1.Parameters.Add("KHVERSTAGE", OracleType.NVarChar).Value = dwData["KHVERSTAGE"].ToString(); 
          or1.Parameters.Add("KHVERSNR", OracleType.NVarChar).Value = dwData["KHVERSNR"].ToString(); 
          or1.Parameters.Add("KHCOM1", OracleType.NVarChar).Value = dwData["KHCOM1"].ToString(); 
          or1.Parameters.Add("KHCOM2", OracleType.NVarChar).Value = dwData["KHCOM2"].ToString(); 
          or1.Parameters.Add("KHWVTERM", OracleType.NVarChar).Value = dwData["KHWVTERM"].ToString(); 
          or1.Parameters.Add("KHFILLER", OracleType.NVarChar).Value = dwData["KHFILLER"].ToString(); 
          or1.Parameters.Add("KHAENZL", OracleType.NVarChar).Value = dwData["KHAENZL"].ToString(); 
          or1.Parameters.Add("KHAENB", OracleType.NVarChar).Value = dwData["KHAENB"].ToString(); 
          or1.Parameters.Add("KHAEND", OracleType.NVarChar).Value = dwData["KHAEND"].ToString(); 
          or1.Parameters.Add("KHNEUB", OracleType.NVarChar).Value = dwData["KHNEUB"].ToString(); 
          or1.Parameters.Add("KHNEUD", OracleType.NVarChar).Value = dwData["KHNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
